//
//  LikeDetailViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/23.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "LikeDetailViewController.h"
#import "baseCollection.h"
#import "XrLayout.h"
#import "LikeDetailCellectionViewCell.h"
#import "ModelOfSecondDetail.h"
#import "APPTools.h"
#import <MJRefresh.h>
@interface LikeDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,XrLayoutItemHeightDelegate>
@property (nonatomic,retain)baseCollection *colectionview;
@property (nonatomic,retain)NSMutableArray *arrData;
@property (nonatomic,assign)NSInteger page;//**< 用来保存请求新数据动态值 */

@end

@implementation LikeDetailViewController

#pragma mark - ------------ 内存管理 ---------------

-(void)dealloc{

    [_colectionview release];
    [_arrData release];
    [_strDetail release];
    [_naviTitle release];
    [super dealloc];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataHandle];
    [self createCollectionView];

    [self refresh];
     self.arrData = [NSMutableArray array];
    self.navigationItem.title = self.naviTitle;
    // Do any additional setup after loading the view.
}

#pragma mark - ------------ 数据解析 ---------------
-(void)dataHandle{
    NSString *strID = [_strDetail substringFromIndex:49];
    
    NSString *str = @"http://203.80.144.212/napi/blog/list/by_category/?cate_key=54589217586df56fb172b4c5&limit=0&locale=zh_CN&screen_height=568&include_fields=sender%2Cfavroite_count%2Calbum%2Cicon_url%2Creply_count%2Clike_count&screen_width=320&start=0&platform_version=9.3.1&app_code=gandalf&platform_name=iPhone%20OS&device_platform=iPhone5%2C2&__domain=www.duitang.com&app_version=6.0.6%20rv%3A156728&device_name=iPhone%205&filter_params=%7B%0A%0A%7D";
    //替换数据
    NSString *url = [str stringByReplacingOccurrencesOfString:@"54589217586df56fb172b4c5" withString:strID];
     NSString *url1 = [url stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _page]];
    
    [APPTools GETWithURL:url1 par:nil success:^(id responseObject) {
        NSArray *arr = responseObject[@"data"][@"object_list"];
        NSArray *arr1 = [ModelOfSecondDetail modelHandleWithArrar:arr];
       
        [self.arrData addObjectsFromArray:arr1];
        
        [self.colectionview reloadData];
    } filed:^(NSError *error) {
        
    }];
   

}

#pragma mark - ------------ 加载 刷新 ---------------

    - (void)refresh
    {
        //刷新
        self.colectionview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _page = 1;
            [self dataHandle];
            [_colectionview.mj_header endRefreshing];
        }];
        
        //加载
        self.colectionview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            _page += 1;
            [self dataHandle];
            [_colectionview.mj_footer endRefreshing];
        }];
        
        
    }








#pragma mark - ------------ 创建collectionView ---------------

-(void)createCollectionView{

    
    XrLayout *layout = [[XrLayout alloc]init];
    //列数
    layout.columCounts = 2;
    //列间距
    layout.columSpace = 10;
    //行间距
    layout.rowSpace = 20;
    //边距
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    //代理
    layout.delegate = self;
    
    self.colectionview = [[baseCollection alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    self.colectionview.delegate = self;
    self.colectionview.dataSource = self;
    self.colectionview.showsVerticalScrollIndicator = YES;
    [self.colectionview registerClass:[LikeDetailCellectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    [self.view addSubview: self.colectionview];
}

#pragma mark - ------------ 赋值 ---------------
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LikeDetailCellectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    ModelOfSecondDetail *model = _arrData[indexPath.item];
    cell.labelContent.text = model.msg;
    cell.model = model;
    
    return cell;
 
    
}
#pragma mark - ------------ 返回高度 ---------------

- (CGFloat)layout:(XrLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width{
   ModelOfSecondDetail *m = _arrData[indexPath.item];
    //图片高度
    CGFloat imgH = width * m.height.doubleValue/ m.width.doubleValue;

    //文字高度
    
    CGFloat h1 = [LikeDetailCellectionViewCell heightWithText:m.msg];
    
    //返回行数
    
    if (h1 >= 15.51 * 6) {
        h1 = 15.51 * 6;
    }
    
    //每个cell的高度 = 图片高度 和 文本高度  + 其他控件高度(80)
    
    return imgH + h1 + 106 ;
    
    return 0;
    

    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arrData.count;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
